// pages/vote/vote.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loading: false,
    disabled: false,
    loadingImg: false,
    percent: false,
    voteDetail: '',
    voteOption: []
  },

  chooseOption(event) {
    let index = event.currentTarget.dataset.index
    let arr = this.data.voteOption
    arr[index]['mode'] = !arr[index]['mode']
    if (arr[index]['mode']) {
      arr[index]['vote_num'] = parseInt(arr[index]['vote_num']) + 1
    } else {
      arr[index]['vote_num'] = parseInt(arr[index]['vote_num']) - 1
    }
    this.setData({
      voteOption: arr
    })

    let total = 0
    this.data.voteOption.forEach((item) => {
      if (item['mode']) total += 1
    })
    console.log(total)
    if (total > 4) {
      arr[index]['mode'] = !arr[index]['mode']
      this.setData({
        voteOption: arr
      })
      wx.showModal({
        title: '提示',
        content: '数量超过选择上限',
        showCancel: false,
        success: res => {
          if (res.confirm) {
            console.log('确认')
          }
        }
      })
    }
  },

  submit() {
    this.setData({
      loading: !this.data.loading,
      percent: !this.data.percent
    })
    let userid = this.data.voteDetail.user_id
    let arr_id = []
    this.data.voteOption.forEach((item) => {
      if (item['mode']) arr_id.push(item.id)
    })
    let standard_id = arr_id.join(',')
    wx.request({
      url: 'https://www.jihe.pro/Api/Vote/vote',
      method: 'POST',
      data: {
        vote_id: this.data.voteDetail.id,
        standard_id: standard_id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'X-USER-ID': userid,
        'X-TOKEN': '5b9fceaded4a9aa469aebeb55438ef0c'
      },
      success: res => {
        console.log(res)

        //计算每个选项百分比
        let checkedNum = 0
        let checkNum = 0
        this.data.voteOption.forEach(item => {
          checkedNum += parseInt(item['vote_num'])
          if (item.mode) {
            checkNum += 1
          }
        })
        let total = checkNum + checkedNum
        let arr = this.data.voteOption
        arr.forEach(item => {
            if (item['mode']) {
            item['ratio'] = (((parseInt(item['vote_num']) + 1) / total) * 100).toFixed(1)
          } else {
            item['ratio'] = ((item['vote_num'] / total) * 100).toFixed(1)
          }
        })
        this.setData({
          voteOption: arr
        })
      }
    })


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    let id = 4
    let userid = 1
    let webUrl = 'https://api.jihe.pro'
    let _this = this
    wx.request({
      url: webUrl + "/v1/vote/" + id + "/" + userid,
      success: res => {
        let _res = res.data
        for (let i in _res.standard) {
          _res.standard[i]['mode'] = false
        }
        _this.setData({
          voteDetail: _res.vote,
          voteOption: _res.standard
        })

        //判断是否超过最后日期
        let end_time = _res.vote['end_time']
        let end = new Date(Date.parse(end_time.replace(/-/g, "/")))
        let cur_time = new Date()
        if (end < cur_time) {
          this.setData({
            disabled: !this.data.disabled
          })
        }
      },
      fail: req => {

      },
      complete: () => {
        wx.hideLoading()
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})